// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register_procedure_module_draft_ok.java

package manufacture.module_balance;

import include.nseer_cookie.counter;
import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;
import validata.ValidataRecord;

public class register_procedure_module_draft_ok extends HttpServlet
{

	public register_procedure_module_draft_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		ValidataRecord vr = new ValidataRecord();
		counter count = new counter(dbApplication);
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String manufacture_ID = request.getParameter("manufacture_ID");
				String product_amount = request.getParameter("product_amount");
				int num = Integer.parseInt(product_amount);
				int p = 0;
				for (int i = 1; i < num; i++)
				{
					String tem_amount = (new StringBuilder("amount")).append(i).toString();
					String amount = request.getParameter(tem_amount);
					if (!validata.validata(amount))
						p++;
				}

				if (p == 0)
				{
					String time = "";
					Date now = new Date();
					SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
					time = formatter.format(now);
					String procedure_name = request.getParameter("procedure_name");
					String procedure_ID = request.getParameter("procedure_ID");
					String register_time = request.getParameter("register_time");
					String procedure_responsible_person = request.getParameter("procedure_responsible_person");
					String register = request.getParameter("register");
					String bodyc = new String(request.getParameter("reason").getBytes("UTF-8"), "UTF-8");
					String reason = exchange.toHtml(bodyc);
					int module_time = 1;
					String sql1 = (new StringBuilder("select module_time from manufacture_module_balance where manufacture_ID='")).append(manufacture_ID).append("' and procedure_name='").append(procedure_name).append("'").toString();
					ResultSet rs1 = manufacture_db.executeQuery(sql1);
					if (rs1.next())
						module_time = rs1.getInt("module_time") + 1;
					sql1 = (new StringBuilder("select * from manufacture_manufacture where manufacture_ID='")).append(manufacture_ID).append("'").toString();
					rs1 = manufacture_db.executeQuery(sql1);
					if (rs1.next())
					{
						String sql = (new StringBuilder("insert into manufacture_module_balance(manufacture_ID,product_ID,product_name,procedure_ID,procedure_name,procedure_responsible_person,reason,module_time,register_time,register,check_tag,excel_tag) values ('")).append(manufacture_ID).append("','").append(rs1.getString("product_ID")).append("','").append(rs1.getString("product_name")).append("','").append(procedure_ID).append("','").append(procedure_name).append("','").append(procedure_responsible_person).append("','").append(reason).append("','").append(module_time).append("','").append(register_time).append("','").append(register).append("','5','2')").toString();
						manufacture_db.executeUpdate(sql);
					}
					double cost_price_sum = 0.0D;
					for (int i = 1; i < num; i++)
					{
						String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
						String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
						String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
						String tem_amount = (new StringBuilder("amount")).append(i).toString();
						String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
						String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
						String product_name = request.getParameter(tem_product_name);
						String product_ID = request.getParameter(tem_product_ID);
						String product_describe = request.getParameter(tem_product_describe);
						String amount = request.getParameter(tem_amount);
						String amount_unit = request.getParameter(tem_amount_unit);
						String cost_price2 = request.getParameter(tem_cost_price);
						StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
						String cost_price;
						String cost_price1;
						for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
							cost_price1 = tokenTO3.nextToken();

						double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(amount);
						cost_price_sum += subtotal;
						String sql2 = (new StringBuilder("insert into manufacture_module_balance_details(manufacture_ID,procedure_name,details_number,product_ID,product_name,product_describe,amount,cost_price,amount_unit,subtotal,register_time,module_time) values ('")).append(manufacture_ID).append("','").append(procedure_name).append("','").append(i).append("','").append(product_ID).append("','").append(product_name).append("','").append(product_describe).append("','").append(amount).append("','").append(cost_price).append("','").append(amount_unit).append("','").append(subtotal).append("','").append(register_time).append("','").append(module_time).append("')").toString();
						manufacture_db.executeUpdate(sql2);
					}

					String sql3 = (new StringBuilder("update manufacture_module_balance set module_cost_price_sum='")).append(cost_price_sum).append("' where manufacture_ID='").append(manufacture_ID).append("' and module_time='").append(module_time).append("' and procedure_ID='").append(procedure_ID).append("'").toString();
					manufacture_db.executeUpdate(sql3);
					response.sendRedirect("manufacture/module_balance/register_procedure_module_draft_ok.jsp?finished_tag=0");
				} else
				{
					response.sendRedirect("manufacture/module_balance/register_procedure_module_draft_ok.jsp?finished_tag=1");
				}
				manufacture_db.commit();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
